package com.lyy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyy.Vo.QueryKeyWord;
import com.lyy.aspect.ControllerEndpoint;
import com.lyy.response.Result;
import com.lyy.system.entity.Log;
import com.lyy.system.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/log")
@Api(value = "日志管理",tags = "操作日志接口")
public class LogController {

    @Autowired
    private LogService logService;

    @PostMapping("/queryLogList")
    @ApiOperation(value = "操作日志分页查询列表",notes = "条件分页查询")
    public Result queryLogList(@RequestParam(required = true,defaultValue = "1") Integer current,
                               @RequestParam(required = true,defaultValue = "6") Integer size,
                               @RequestBody QueryKeyWord queryKeyWord){
        Page<Log> page =new Page<>(current,size);
        QueryWrapper<Log> wrapper =new QueryWrapper<>();
        if (queryKeyWord!=null){
            if (!StringUtils.isEmpty(queryKeyWord.getKeyWord())){
                wrapper.eq("username",queryKeyWord.getKeyWord());
            }
            if (!StringUtils.isEmpty(queryKeyWord.getKeyWord1())){
                wrapper.like("operation",queryKeyWord.getKeyWord1());
            }
            if (!StringUtils.isEmpty(queryKeyWord.getKeyWord2())){
                wrapper.eq("ip",queryKeyWord.getKeyWord2());
            }
        }
        IPage<Log> logIPage = logService.queryLogList(page,wrapper);
        Long total=logIPage.getTotal();
        List<Log> records=logIPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    @ControllerEndpoint(exceptionMessage = "删除操作日志失败", operation = "删除操作日志")
    @DeleteMapping("/delLogById")
    @ApiOperation(value = "删除日志",notes = "删除操作日志信息")
    public Result delLogById(Long id){
        logService.delLogById(id);
        return Result.ok();
    }
}
